package com.taplinker.client.im.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.ImageUtil;

public class GridViewAdapter extends BaseAdapter {
	public static final String TAG = "GridViewAdapter";
	private Context context;
	private List<Integer> mList;

	public GridViewAdapter(Context context, int page) {
		this.context = context;
		mList = new ArrayList<Integer>();

		int i = page * SmileyParser.PERPAGE_IMAGE_SIZE;
		int iEnd = i + SmileyParser.PERPAGE_IMAGE_SIZE;
		while ((i < SmileyParser.DEFAULT_SMILEY_RES_IDS.length) && (i < iEnd)) {
			mList.add(SmileyParser.DEFAULT_SMILEY_RES_IDS[i]);
			i++;
		}
	}

	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView i;
		if (convertView == null) {
			i = new ImageView(context);
			i.setScaleType(ImageView.ScaleType.FIT_CENTER);

			int widthAndHeight = ImageUtil.dip2px(context, 40);
			i.setLayoutParams(new GridView.LayoutParams(widthAndHeight, widthAndHeight));
		} else {
			i = (ImageView) convertView;
		}
		i.setImageResource(mList.get(position));

		return i;
	}

}
